package vinux.web.manager.controller;

import java.sql.SQLFeatureNotSupportedException;
import java.util.List;
import java.util.Map;

import org.nlpcn.es4sql.exception.SqlParseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import net.sf.json.JSONArray;
import vinux.service.common.base.BaseController;
import vinux.service.common.statics.Constant;
import vinux.service.common.statics.LogConstant;
import vinux.service.common.statics.SysConstant;
import vinux.service.common.util.CollectionUtils;
import vinux.service.common.util.api.APIUtils;
import vinux.service.core.elasticsearch.utils.ElasticsearchManager;
import vinux.service.core.utils.SystemLogUitls;
import vinux.service.entity.expenseacount.ExpenseAcount;
import vinux.service.entity.userbaseinfo.UserBaseInfo;
import vinux.service.service.expenseacount.service.ExpenseAcountService;
import vinux.service.service.tchange.service.TChangeService;
import vinux.service.service.userbaseinfo.service.UserBaseInfoService;
import vinux.web.common.es.SearchUtil;
import vinux.web.common.es.bo.ExpenseAcountBo;
import vinux.web.common.es.bo.UserInfoBo;



/**
 * 
 * @ClassName EsSyncController.java	
 * @Descreption 将mysql中的数据一键同步到Es 
 * @author 苏超
 * 2016年11月8日
 */
@Controller()
@Scope(Constant.SCOPE)
@RequestMapping("sync/syncmysql2es")
public class EsSyncController extends BaseController{
	
	@Autowired
	TChangeService changeService;
	
	@Autowired
	ExpenseAcountService expenseService;
	
	@Autowired
	UserBaseInfoService userService;

	/**
	 * 
	 * @Description  将mysql中 EXPENSE_ACOUNT 表的数据一键同步到ES
	 * @return 
	 * @param
	 * @author suchao
	 * @Date 2016年11月8日
	 */
	@RequestMapping("expenseacount")
	@ResponseBody()
	public Map<String,Object> syncMysql2Es(){
		Exception methodMessage = null;
		Map<String,Object> result = CollectionUtils.newHashMap();
		List<ExpenseAcount> expenseList = CollectionUtils.newArrayList();
		List<ExpenseAcountBo> expenseBoList = CollectionUtils.newArrayList();
		try{
			expenseList = expenseService.selectAllExpense();
			for(ExpenseAcount expense : expenseList){
				ExpenseAcountBo expensebo = new ExpenseAcountBo(expense);
				expenseBoList.add(expensebo);
			}
			JSONArray expenseBoJArray  = JSONArray.fromObject(expenseBoList);
			ElasticsearchManager.save(SysConstant.MANAGER_INDEX, SysConstant.MANAGER_TYPE_EXPENSE_ACOUNT, "id", expenseBoJArray);
			result.put("data", expenseBoJArray);
			result.put("status",200);
			result.put("message", "Es数据同步完成");
		}catch(Exception e){
			result.put("status", 500);
			result.put("message", "Es数据同步异常");
			methodMessage = e;
		}finally{
			SystemLogUitls.info(null, null,
					result.toString(), APIUtils.class,
					"", methodMessage,
					LogConstant.DATA_DEAL_READ,
					LogConstant.SYSTEM_YANGLAO, "养老管理后台同步ExpenseAcount数据到Es", SysConstant.SYNC_SYNCMYSQL2ES_EXPENSEACOUNT,
					LogConstant.OTHER_LOG_COMMON);
		}
		return result;
	}
	
	
	/**
	 * 
	 * @Description 将mysql中 USER_BASE_INFO 表中的数据 同步到 ES 
	 * @return 
	 * @param
	 * @author suchao
	 * @Date 2016年11月8日
	 */
	@RequestMapping("userbaseinfo")
	@ResponseBody()
	public Map<String,Object> syncUserBaseInfo(){
		Exception methodMessage = null;
		Map<String,Object> result = CollectionUtils.newHashMap();
		List<UserBaseInfo> userList = CollectionUtils.newArrayList();
		List<UserInfoBo> userBoList = CollectionUtils.newArrayList();
		try{
			userList = userService.selectAllPensionUser();
			for(UserBaseInfo user : userList){
				UserInfoBo userBo = new UserInfoBo(user);
				userBoList.add(userBo);
			}
			JSONArray userBoJArray  = JSONArray.fromObject(userBoList);
			ElasticsearchManager.save(SysConstant.MANAGER_INDEX, SysConstant.MANAGER_TYPE_USER_BASE_INFO, "id", userBoJArray);
			result.put("data", userBoJArray);
			result.put("status",200);
			result.put("message", "Es数据同步完成");
		}catch(Exception e){
			result.put("status", 500);
			result.put("message", "Es数据同步异常");
			methodMessage = e;
		}finally{
			SystemLogUitls.info(null, null,
					result.toString(), APIUtils.class,
					"", methodMessage,
					LogConstant.DATA_DEAL_READ,
					LogConstant.SYSTEM_YANGLAO, "养老管理后台同步UserBaseInfo数据到Es", SysConstant.SYNC_SYNCMYSQL2ES_USERBASEINFO,
					LogConstant.OTHER_LOG_COMMON);
		}
		return result;
	}
	
	
	@RequestMapping("deleteuserinfo")
	@ResponseBody()
	public Map<String,Object> deleteEsUserBaseInfo(){
		Exception methodMessage = null;
		changeService.selectAllUnSync();
		Map<String,Object> result = CollectionUtils.newHashMap();
		String sql = "delete from " + SysConstant.MANAGER_INDEX + "/" + SysConstant.MANAGER_TYPE_USER_BASE_INFO ;
		try {
			SearchUtil.delete(sql);
			result.put("status",200);
			result.put("message", "清除Es userbaseInfo数据成功");
		}catch (SQLFeatureNotSupportedException | SqlParseException e1) {
			result.put("status", 500);
			result.put("message", "清除Es userbaseInfo数据失败");
			methodMessage = e1;
		}finally{
			SystemLogUitls.info(null, null,
					result.toString(), APIUtils.class,
					"", methodMessage,
					LogConstant.DATA_DEAL_READ,
					LogConstant.SYSTEM_YANGLAO, "删除ES中 UserbaseInfo 数据", SysConstant.SYNC_SYNCMYSQL2ES_DELETEESUSERBASEINFO,
					LogConstant.OTHER_LOG_COMMON);
		}
		return result;
	}
	

	@RequestMapping("deleteexpenseacount")
	@ResponseBody()
	public Map<String,Object> deleteEsExpenseAcount(){
		Exception methodMessage = null;
		Map<String,Object> result = CollectionUtils.newHashMap();
		String sql = "delete from " + SysConstant.MANAGER_INDEX + "/" + SysConstant.MANAGER_TYPE_USER_BASE_INFO ;
		try {
			SearchUtil.delete(sql);
			result.put("status",200);
			result.put("message", "清除Es userbaseInfo数据成功");
		}catch (SQLFeatureNotSupportedException | SqlParseException e1) {
			result.put("status", 500);
			result.put("message", "清除Es userbaseInfo数据失败");
			methodMessage = e1;
		}finally{
			SystemLogUitls.info(null, null,
					result.toString(), APIUtils.class,
					"", methodMessage,
					LogConstant.DATA_DEAL_READ,
					LogConstant.SYSTEM_YANGLAO, "删除ES中 UserbaseInfo 数据", SysConstant.SYNC_SYNCMYSQL2ES_DELETEES_ESPENSE,
					LogConstant.OTHER_LOG_COMMON);
		}
		return result;
	}
	
}
